#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: helpers.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: utils gui functions for vazaar

import gtk

def set_model_combobox_from_list (cb, items, bitems):
    """Setup a ComboBox or ComboBoxEntry based on a list of strings
    where:
       items is a list of keys
       bitems is the text corresponding to its key

    Modified from pygtk faq recipe:
    http://faq.pygtk.org/index.py?req=show&file=faq16.008.htp
    """


    try:
        cb.clear()
        model = gtk.ListStore(str, str)
        cb.set_model(model)

        for i in range(len(items)):
            model.append((items[i], bitems[i]))

        cb.set_active(0)
    except Exception, error:
        pass

    if type(cb) == gtk.ComboBoxEntry:
        cb.set_text_column(1)
    elif type(cb) == gtk.ComboBox:
        cell = gtk.CellRendererText()
        cb.pack_start(cell, True)
        cb.add_attribute(cell, 'text', 1)


def pixbuf_column(name, pos, data_func):
    cr = gtk.CellRendererPixbuf()
    column = gtk.TreeViewColumn(name, cr)
    column.set_sort_column_id(pos)
    column.set_cell_data_func(cr, data_func)
    return column

def text_column(name, pos):
    column = gtk.TreeViewColumn(name, gtk.CellRendererText(), text=pos)
    column.set_sort_column_id(pos)
    column.set_resizable(True)
    return column

def markup_column(name, pos, data_func):
    cr = gtk.CellRendererText()
    column = gtk.TreeViewColumn(name, cr)
    column.set_sort_column_id(pos)
    column.set_cell_data_func(cr, data_func)
    column.set_resizable(True)
    return column

def toggle_column(name, pos, callback):
    cr = gtk.CellRendererToggle()
    cr.connect('toggled', callback)
    column = gtk.TreeViewColumn(name, cr, active=pos)
    column.set_sort_column_id(pos)
    return column
